﻿using AutoMapper;
using Inventory.API.Application.Command.WarehouseManagementCommand;
using Inventory.Errorcode;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using MediatR;

namespace Inventory.API.Application.CommandHandler.WarehouseManagementCommandHandler
{
    public class DeleteWarehousingCommandHanlder : IRequestHandler<DeleteWarehousingCommand, ApiResult>
    {
		private readonly ILogger<DeleteWarehousingCommandHanlder> _logger;
		private readonly IWarehousemanagementRepository repository;
		private readonly IMapper mapper;

		public DeleteWarehousingCommandHanlder(ILogger<DeleteWarehousingCommandHanlder> logger, IWarehousemanagementRepository repository, IMapper mapper)
		{
			_logger = logger;
			this.repository = repository;		
			this.mapper = mapper;
		}
        public async Task<ApiResult> Handle(DeleteWarehousingCommand request, CancellationToken cancellationToken)
        {
			try
			{
				var warehousing = await repository.GetAsync(x => x.Warehousemanagementid == request.Warehousemanagementid);
				if (warehousing == null)
				{
					return ApiResult.Fail("未找到相对于id", StatusCodeEnum.NotFound);
				}
				else
				{
					warehousing.IsDeleted = true;
					await repository.UpdateAsync(warehousing);
					return ApiResult.Success(StatusCodeEnum.Success);
				}
			   
			}
			catch (Exception ex)
			{
				_logger.LogError(ex, "Error Delete Warehousing");
				throw;
			}
        }
    }
}
